﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Collections;
using System.IO;
using System.Net;
using System.Threading.Tasks;

namespace Spider
{
    public partial class Form1 : Form
    {

        List<string> content = new List<string>();

        List<string> name = new List<string>();
        int i = 1;
        int count = 400;
        public Form1()
        {
            InitializeComponent();

            //    webBrowser1.Url = new Uri("http://www.eden.net.cn/SearchResult.aspx?sKey=1035985&SearchTypeID=6");
            //webBrowser1.Url = new Uri("http://www.eden.net.cn/SearchHighResult.aspx?CountryID=0&ShengID=&ShiID=&ACountryID=0&AShengID=&AShiID=&ZAgeB=-1&ZAgeT=-1&ZSgB=-1&ZSgT=-1&ZTzB=-1&ZTzT=-1&Sex=1&IsImg=0&CheckBlood=1&ZBloodType=-1&MinZu=-1&MinZuOther=&XueLi=-1&WaiYu=-1&WaiYuGrade=-1&ZhiYe=-1&ZhiYeOther=&ZhiWu=-1&ZhiWuOther=&WorkMoney=-1&DanWeiXingZhi=-1&MarryType=-1&MarryZhuFang=-1&CheckShuXiang=1&ShuXiang=-1&CheckXingZuo=1&XingZuo=-1&ZongJiaoXinYang=-1&ShengYuGuanNian=-1&OtherMarry=-1&TimeType=-1");
            //webBrowser1.Url = new Uri("http://www.eden.net.cn/SearchHighResult.aspx?CountryID=0&ShengID=&ShiID=&ACountryID=0&AShengID=&AShiID=&ZAgeB=-1&ZAgeT=-1&ZSgB=-1&ZSgT=-1&ZTzB=-1&ZTzT=-1&Sex=0&IsImg=0&CheckBlood=1&ZBloodType=-1&MinZu=-1&MinZuOther=&XueLi=-1&WaiYu=-1&WaiYuGrade=-1&ZhiYe=-1&ZhiYeOther=&ZhiWu=-1&ZhiWuOther=&WorkMoney=-1&DanWeiXingZhi=-1&MarryType=-1&MarryZhuFang=-1&CheckShuXiang=1&ShuXiang=-1&CheckXingZuo=1&XingZuo=-1&ZongJiaoXinYang=-1&ShengYuGuanNian=-1&OtherMarry=-1&TimeType=-1");

            // webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);


        }

        void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            var aa = webBrowser1.DocumentText;

            string reg1 = @"个人独白：.*</p>";

            Regex regex = new Regex(reg1);
            MatchCollection matches = regex.Matches(aa);

            IEnumerator enu = matches.GetEnumerator();
            while (enu.MoveNext() && enu.Current != null)
            {
                Match match = (Match)(enu.Current);
                string cc = match.Value.Replace("</p>", "").Replace("个人独白：", "");

                var aaa = cc.ToArray().Distinct();

                if (cc.Length > 20 && cc.ToArray().Distinct().Count() > 20 && !content.Contains(cc))
                    content.Add(cc);
            }


            IEnumerator enu2 = new Regex("昵称:.*</font>").Matches(aa).GetEnumerator();
            while (enu2.MoveNext() && enu2.Current != null)
            {
                Match match = (Match)(enu2.Current);
                string cc = match.Value.Replace("</font>", "").Replace("昵称:", "");

                if (!name.Contains(cc))
                    name.Add(cc);

                label2.Text = "name:" + name.Count;
            }

            object[] obj = { "pager2", i };
            if (i++ < count)
                webBrowser1.Document.InvokeScript("__doPostBack", obj);

            label1.Text = i.ToString() + "===" + content.Count;




            if (i > count) MessageBox.Show("over == " + content.Count);



        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (var item in content)
            {
                string insert = string.Format("insert into content values('{0}')", item);
                SQLHelper.ExecuteNonQuery(insert);
            }

            foreach (var item in name)
            {
                string insert = string.Format("insert into name values('{0}')", item);
                SQLHelper.ExecuteNonQuery(insert);
            }

            MessageBox.Show("save success.");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string line = string.Empty;
            StreamReader reader = new StreamReader("../../q.txt");
            while ((line = reader.ReadLine()) != null)
            {
                string insert = string.Format("insert into qq(qq) values('{0}')", line);
                SQLHelper.ExecuteNonQuery(insert);
            }

            MessageBox.Show("insert qq success.");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            List<string> city = new List<string>();
            string line = string.Empty;
            StreamReader reader = new StreamReader("../../TextFile1.txt");
            while ((line = reader.ReadLine()) != null)
                city.Add(line);

            string query = "select rowid,*  from qq";
            DataTable dt = SQLHelper.ExecuteDataset(query).Tables[0];

            int i = 0;

            foreach (DataRow row in dt.Rows)
            {
                if (i == city.Count - 1)
                    i = 0;

                string update = "update qq set city='{0}' where rowid={1}";
                SQLHelper.ExecuteNonQuery(string.Format(update, city[i++], row[0].ToString()));


            }

            MessageBox.Show("update qq city success.");

        }

        static List<string> result = new List<string>();
        string insert = "insert into content values('{0}')";
        private void button4_Click(object sender, EventArgs e)
        {
            string path = @"http://okcao.com/woman.asp?KeyWord=&InfoType=2&Age=0&HasPhoto=&sheng=&city=%B1%B1%BE%A9&page=";

            new Task(() =>
            {
                for (int i = 1; i < 20; i++)
                {
                    string pp = path + i;
                    Spider(pp);
                }
            }).Start();

            new Task(() =>
            {
                for (int i = 21; i < 40; i++)
                {
                    string pp = path + i;
                    Spider(pp);
                }
            }).Start();

            new Task(() =>
            {
                for (int i = 41; i < 60; i++)
                {
                    string pp = path + i;
                    Spider(pp);
                }
            }).Start();

            new Task(() =>
            {
                for (int i = 61; i < 80; i++)
                {
                    string pp = path + i;
                    Spider(pp);
                }
            }).Start();

            new Task(() =>
            {
                for (int i = 81; i < 98; i++)
                {
                    string pp = path + i;
                    Spider(pp);
                }
            }).Start();

            new Task(() =>
            {
                for (int i = 99; i < 130; i++)
                {
                    string pp = path + i;
                    Spider(pp);
                }
            }).Start();

            new Task(() =>
            {
                for (int i = 131; i < 150; i++)
                {
                    string pp = path + i;
                    Spider(pp);
                }
            }).Start();

            new Task(() =>
            {
                for (int i = 151; i < 170; i++)
                {
                    string pp = path + i;
                    Spider(pp);
                }
            }).Start();

          


        }

        public void Spider(string path)
        {

            WebClient client = new WebClient();
            byte[] buffer = client.DownloadData(path);
            string content = System.Text.Encoding.UTF8.GetString(buffer);

            MemoryStream stream = new MemoryStream(buffer);
            StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("GB2312"));

            string line;


            while ((line = reader.ReadLine()) != null)
            {
                if (line.Contains("<strong>交友意向：</strong>"))
                    SQLHelper.ExecuteNonQuery(string.Format(insert, reader.ReadLine().Replace("'", "")));
            }

            InsertName(path);

        }

        private void button5_Click(object sender, EventArgs e)
        {

            string path = @"http://okcao.com/woman.asp?KeyWord=&InfoType=2&Age=0&HasPhoto=&sheng=&city=%C9%CF%BA%A3&page=";

            new Task(() =>
            {
                for (int i = 1; i < 20; i++)
                {
                    string pp = path + i;
                    InsertName(pp);
                }
            }).Start();

            new Task(() =>
            {
                for (int i = 21; i < 40; i++)
                {
                    string pp = path + i;
                    InsertName(pp);
                }
            }).Start();

            new Task(() =>
            {
                for (int i = 41; i < 60; i++)
                {
                    string pp = path + i;
                    InsertName(pp);
                }
            }).Start();

            new Task(() =>
            {
                for (int i = 61; i < 80; i++)
                {
                    string pp = path + i;
                    InsertName(pp);
                }
            }).Start();

            new Task(() =>
            {
                for (int i = 81; i < 98; i++)
                {
                    string pp = path + i;
                    InsertName(pp);
                }
            }).Start();





        }

        void InsertName(string path)
        {
            WebClient client = new WebClient();
            byte[] buffer = client.DownloadData(path);
            string content = System.Text.Encoding.UTF8.GetString(buffer);

            MemoryStream stream = new MemoryStream(buffer);
            StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("GB2312"));

            string line;


            while ((line = reader.ReadLine()) != null)
            {
                if (line.Contains("<span title='人气：'></span>"))
                {
                    string name = new Regex("<b>.*</b>").Match(line).Value.Replace("<b>", "").Replace("</b>", "");
                    SQLHelper.ExecuteNonQuery(string.Format("insert into name values('{0}')", name.Replace("'", "")));
                }
            }

        }

        private void button6_Click(object sender, EventArgs e)
        {
           //FileInfo file =new FileInfo(
        }

    }
}
